/*
 * StringUtil.java
 *
 * Created on 20 October 2007, 11:10
 *
 */
package net.ubinow.mflex.util;

import java.util.Vector;

/**
 *
 * @author Josh
 */
public class StringUtil {

    //<editor-fold defaultstate="collapsed" desc="singleton fields and methods">
    public static StringUtil _instacne = null;

    public static StringUtil getInstance() {
        if (_instacne == null) {
            _instacne = new StringUtil();
        }

        return _instacne;
    }
    //</editor-fold>
    private StringUtil() {
    }

    //<editor-fold defaultstate="collapsed" desc="helper methods">
    /**
     * Split string into multiple strings
     * @param original      Original string
     * @param separator     Separator string in original string
     * @return              Splitted string array
     */
    public String[] split(String original, String separator) {
        Vector nodes = new Vector();

        // Parse nodes into vector
        int index = original.indexOf(separator);
        while (index >= 0) {
            nodes.addElement(original.substring(0, index));
            original = original.substring(index + separator.length());
            index = original.indexOf(separator);
        }
        // Get the last node
        nodes.addElement(original);

        // Create splitted string array
        String[] result = new String[nodes.size()];
        if (nodes.size() > 0) {
            for (int loop = 0; loop < nodes.size(); loop++) {
                result[loop] = (String) nodes.elementAt(loop);
            }
        }
        return result;
    }

    public String replace(String original, String find, String replace) {
        String result = null;
        boolean found = false;
        int index = original.indexOf(find);
        while (index > -1) {
            result = original.substring(0, index) + replace + original.substring(index + find.length());
            original = result;
            index = result.indexOf(find);
            found = true;
        }
        if (!found) {
            result = original;
        }
        return result;
    }

    public String encode(String str) {
        String[] values = {" ", "&", "?", "+"};
        String[] encoded_values = {"%20", "%26", "%3F", "%2B"};

        String encodedStr = str;
        int number = values.length;
        for (int i = 0; i < number; i++) {
            encodedStr = replace(encodedStr, values[i], encoded_values[i]);
        }

        return encodedStr;
    }

    /**
     * Create a string repeating a given one until it gets of at least a given
     * length.<br/>
     * For example, repeat( &quot;abc&quot;, 7 ) will return &quot;abcabcabc&quot;.
     * @param s source string to repeat
     * @param length length to achieve
     * @return repeated string.
     */
    public String repeat(final String s, final int length) {
        final StringBuffer buffer = new StringBuffer();
        while (buffer.length() < length) {
            buffer.append(s);
        }
        return buffer.toString();
    }

    public void sort( String[] arr ) {
        if (arr == null) {
            return;
        }

        for (int i = arr.length; --i >= 0;) {
            boolean flipped = false;
            for (int j = 0; j < i; j++) {
                if (arr[j].compareTo(arr[j + 1]) > 0) {
                    String tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;

                    flipped = true;

                }
            }

            if (!flipped) {
                break;
            }
        }

    }
    
    //</editor-fold>
}
